#include <iostream>
using namespace std;

struct Array{
	int exp;
	int coef;
};
struct Node{
	int exp;
	int coef;
	struct Node *next;	 
};
void Create(Node *&LA, Array a[], int x, Node *&LB, Array b[], int y);
void add(Node *LA, Node *LB, Node *&LC);
void Output(Node *LC);
void mul(Node *LA, Node *LB, Node *&LD);
void sort(Node *&LD);
void quchong(Node *&LD);
int main(){
	Array a[666], b[666];
	int e, c;
	Node *LA, *LB, *LC, *LD;
	int k = 0;
	while(cin >> c >> e){
		if(e != 0 || c != 0){
			a[k].coef = c;
			a[k].exp = e;
			k++;
		}
		else
			break;
	}
	int j = 0;
	while(cin >> c >> e){
		if(e != 0 || c != 0){
			b[j].coef = c;
			b[j].exp = e;
			j++;
		}
		else
			break;
	}
	Create(LA,a,k,LB,b,j);
	add(LA,LB,LC);
	Output(LC);
	mul(LA, LB, LD);
	sort(LD);
	quchong(LD);
	Output(LD);
	return 0;
}
void Create(Node *&LA, Array a[], int x, Node *&LB, Array b[], int y){
	Node *s, *r;
	LA = new Node;
	r = LA;
	for(int i = 0; i < x; i++){
		s = new Node;
		s->coef = a[i].coef;
		s->exp = a[i].exp;
		r->next = s;
		r = s;
	}
	r->next = NULL;
	
	LB = new Node;
	r = LB;
	for(int i = 0; i < y; i++){
		s = new Node;
		s->coef = b[i].coef;
		s->exp = b[i].exp;
		r->next = s;
		r = s;
	}
	r->next = NULL;
}
void add(Node *LA, Node *LB, Node *&LC){
	Node *p = LA->next;
	Node *q = LB->next;
	Node *s, *r;
	LC = new Node;
	r = LC;
	int c = 0;
	while(p != NULL && q != NULL){
		if(p->exp > q->exp){
			s = new Node;
			s->coef = p->coef;
			s->exp = p->exp;
			r->next = s;
			r = s;
			p = p->next;
		}
		else if(p->exp < q->exp){
			s = new Node;
			s->coef = q->coef;
			s->exp = q->exp;
			r->next = s;
			r = s;
			q = q->next;
		}
		else{
			c = p->coef + q->coef;
			if(c!= 0){
				s = new Node;
				s->exp = q->exp;
				s->coef = c;
				r->next = s;
				r = s;	
			}
			p = p->next;
			q = q->next;
		}
	}
	while(p != NULL){
		s = new Node;
		s->coef = p->coef;
		s->exp = p->exp;
		r->next = s;
		r = s;
		p = p->next;
	} 
	while(q != NULL){
		s = new Node;
		s->coef = q->coef;
		s->exp = q->exp;
		r->next = s;
		r = s;
		q = q->next;
	}
	r->next = NULL;
}
void Output(Node *LC){
	Node *p = LC->next;
	int i = 1;
	while(p != NULL){
		if(i)
			i = 0;
		else if(p->coef > 0)
			cout << "+";
		if(p->exp == 0)
			cout << p->coef;
		else if(p->coef == -1)
			cout << "-x";
		else if(p->coef == 1)
			cout << "x";
		else
			cout << p->coef << "x";
		if(p->exp != 1 && p->exp != 0)
			cout << "^" << p->exp;
		p = p->next;
	}
	cout << endl;
}
void mul(Node *LA, Node *LB, Node *&LD){
	Node *p = LA->next;
	Node *q = LB->next;
	Node *s, *r;
	LD = new Node;
	r = LD;
	while(p != NULL){
		q = LB->next;
		while(q != NULL){
			s = new Node;
			s->coef = p->coef * q->coef;
			s->exp = p->exp + q->exp;
			r->next = s;
			r = s;
			q = q->next;
		}
		p = p->next;
	}
	r->next = NULL;
} 
void sort(Node *&LD){
	Node *p = LD->next;
	Node *l, *r;
	if(p!=NULL){
		r = p->next;
		p->next = NULL;
		p = r;
		while(p!=NULL){
			r = p->next;
			l = LD;
			while(l->next!=NULL && l->next->exp > p->exp)
				l = l->next;
			p->next = l->next;
			l->next = p;
			p = r;	
		}
	}
}
void quchong(Node *&LD){
	Node *p = LD->next, *q;
	while(p->next!=NULL){
		if(p->exp == p->next->exp){
			p->coef = p->coef + p->next->coef;
			q = p->next;
			p->next = q->next;
			delete q;
		}
		else
			p = p->next;
	}
}
